//
//  ViewController.swift
//  JCNewsForMac
//
//  Created by ifly on 13/7/18.
//  Copyright © 2018年 Ismail. All rights reserved.
//  https://blog.csdn.net/lovechris00/article/details/72779076  mac 教程

import Cocoa
import SnapKit

class ViewController: NSViewController {
    
    var jcUserNameText = NSTextField()
    var jcPassWordText = NSTextField()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        jcAddSubView()
    }
    
    
    func jcAddSubView() -> Void {
        
        view.addSubview(jcUserNameText)
        view.addSubview(jcPassWordText)
        
        let userName = NSTextField()
        view.addSubview(userName)
        
        let password = NSTextField()
        view.addSubview(password)
        
        
        let jcOKBtn = NSButton.init(title: "立即登录", target: self, action: #selector(jcGoLogin))
        view.addSubview(jcOKBtn)
        jcOKBtn.setButtonType(.momentaryLight)
        
        userName.stringValue = "用户名:"
        userName.jcMakeLable(16)
        userName.alignment = .right
        
        
        password.stringValue = "密\t码:"
        password.jcMakeLable(16)
        password.alignment = .right
        
        jcUserNameText.placeholderString = "请输入用户名"
        jcPassWordText.placeholderString = "请输入密码"

        
        userName.snp.makeConstraints { (make) in
            make.right.equalTo(jcUserNameText.snp.left).offset(-10);
            make.top.equalTo(jcUserNameText)
            make.size.equalTo(CGSize.init(width: 100, height: 30))
        }
        
        password.snp.makeConstraints { (make) in
            make.right.equalTo(jcPassWordText.snp.left).offset(-10)
            make.top.equalTo(jcPassWordText)
            make.size.equalTo(userName)
        }
        
        jcUserNameText.snp.makeConstraints { (make) in
            make.centerX.equalTo(view).offset(10)
            make.centerY.equalTo(view).offset(-30)
            make.size.equalTo(CGSize.init(width: 180, height: 30))
        }
        
        jcPassWordText.snp.makeConstraints { (make) in
            make.top.equalTo(jcUserNameText.snp.bottom).offset(10)
            make.left.equalTo(jcUserNameText)
            make.size.equalTo(jcUserNameText)

        }
        
        jcOKBtn.snp.makeConstraints { (make) in
            make.top.equalTo(jcPassWordText.snp.bottom).offset(15)
            make.centerX.equalTo(jcPassWordText)
            make.size.equalTo(CGSize.init(width: 120, height: 30))
        }
        
    }
    
    @objc func jcGoLogin(){
        
        JCAPI.jcLogIn(jcUserNameText.stringValue, password: jcPassWordText.stringValue, success: { (jsonData) in

            JCUserModel.saveUser(fordDic: jsonData as NSDictionary)
            
            let storyboard = NSStoryboard.init(name: NSStoryboard.Name.init("Main"), bundle: nil)
            let vc:NSWindowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier.init("JCListWindowController")) as! NSWindowController
            vc.showWindow(nil)
            self.view.window?.orderOut(nil)
            

        }) { (errStr, errCode) in
            
            let alert = NSAlert.init()
            alert.addButton(withTitle: "确定")
            alert.messageText = "系统提示"
            alert.informativeText = errStr
            alert.alertStyle = NSAlert.Style.informational
            alert.beginSheetModal(for: self.view.window!, completionHandler: {
                print("===========",$0)
            })
            
        }
        
    }
    


    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}



